package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.wode.ProductNewActivity;

import java.util.HashMap;
import java.util.List;

public class ProductTuijianView extends RelativeLayout {
    private Context context;
    public ProductTuijianView(Context context) {
        this(context,null);
    }

    public ProductTuijianView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ProductTuijianView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    ProductTuijianSIngleView first;
    ProductTuijianSIngleView second;
    ProductTuijianSIngleView third;
    ProductTuijianSIngleView four;

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.product_tuijian,this);
        first = findViewById(R.id.first);
        second = findViewById(R.id.second);
        third = findViewById(R.id.third);
        four = findViewById(R.id.four);
    }

    public void initData(long id) {
//         "brandId=" + "1" +id +
        String url = DataManager.BaseUrl + "mall/shopproductinfo/getSkuDetails?" +
                "brandId=" +  +id +
                "&current=1&size=10";
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(getContext(), url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    TuijianBean tuijianBean = JSON.parseObject(response, TuijianBean.class);
                    List<TuijianBean.DataBean.RecordsBean> records = tuijianBean.getData().getRecords();
                    System.out.println();
                    setData(records);
                }catch (Exception e){
                    //todo 暂时去除空界面显示
//                    setNUllDataView(true);
                }
            }
        });
    }

    private void setData(List<TuijianBean.DataBean.RecordsBean> records) {
        try {
            for (int i = 0; i < records.size(); i++) {
                TuijianBean.DataBean.RecordsBean recordsBean = records.get(i);
                if(i == 0){
                    setSingleData(first,recordsBean);
                }else if(i == 1){
                    setSingleData(second,recordsBean);
                }else if(i == 2){
                    setSingleData(third,recordsBean);
                }else if(i == 3){
                    setSingleData(four,recordsBean);
                }

            }
        }catch (Exception e){

        }
    }

    private void setSingleData(ProductTuijianSIngleView view, TuijianBean.DataBean.RecordsBean recordsBean) {
        view.setVisibility(VISIBLE);
        view.setData(recordsBean);

        //todo 暂时没有产品Id
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ProductNewActivity.class);
                intent.putExtra("id",recordsBean.getId() + 0L);
                intent.putExtra("brandId",recordsBean.getBrandId() + 0L);
                context.startActivity(intent);
            }
        });
    }
}
